<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/18
 * Time: 13:48
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\common\business\OrderDetail;
use app\common\business\PlaceOrder as BusObj;
use app\common\business\SeatDetail;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\Exception;

class Order extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        $this->busObj = new BusObj();
        parent::__construct($app);
    }

    public function addOrder()
    {
        $param = $this->request->param();
        try {
            $res = $this->busObj->add($param);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        if ($res) {
            return Show::json_show(Status::mini_normal, "操作成功");
        } else {
            return Show::json_show(Status::mini_error, "操作失败");
        }
    }

    /**
     * @return \think\response\Json
     * @throws Exception
     */
    public function setOrder()
    {
        $oldArray = $this->request->param('oldArray', '', '');
        $theatreId = $this->request->param('theatreId', 0, 'intval');
        $openid = $this->request->param('openid', '', 'trim');
        if (empty($oldArray) || $theatreId == 0) {
            return Show::json_show(Status::mini_error, "内部错误");
        }
        //查询座位是否被抢
        try {
            $orderDetail = (new OrderDetail())->getOrderDetailSeatById($theatreId, $oldArray);
        } catch (\Exception $e) {
            throw new Exception("内部错误");
        }
        if (!empty($orderDetail)) {
            $ids = array_column($orderDetail, 'seat_id');
            try {
                $detail = (new SeatDetail())->getSeatDetailByIds($ids);
            } catch (\Exception $e) {
                return Show::json_show(Status::mini_error, $e->getMessage());
            }
            if (!empty($detail)) {
                $str = "";
                foreach ($detail as $v) {
                    $str .= $v['seatY'] . "排" . $v['seatX'] . "座 ";
                }
                $str .= " 已经被选";
                return Show::json_show(Status::mini_normal, "ok", [
                    "msg" => $str,
                    "code" => 99
                ]);
            }
        }
        try {
            $order_id = $this->busObj->setOrderByData($theatreId, $oldArray, $openid);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", [
            "msg" => $order_id,
            "code" => 1
        ]);
    }

    /**
     * @return \think\response\Json
     */
    public function getOrderById()
    {
        $openid = $this->request->param('openid', '', 'trim');
        $order_id = $this->request->param('order_id', '', 'trim');
        if (empty($order_id)) {
            return Show::json_show(Status::mini_error, "订单返回异常");
        }
        try {
            $order = $this->busObj->getOrderByIdMoney($order_id, $openid);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $order);
    }

    public function confirmPayment()
    {
        $order_id = $this->request->param('order_id', '', 'trim');
        if (empty($order_id)) {
            return Show::json_show(Status::mini_error, "订单错误");
        }
        $mobile = $this->request->param('mobile', '', 'trim');
        if (!preg_match('/^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1|8|9]))\d{8}$/', $mobile)) {
            return Show::json_show(Status::mini_error, "手机号不正确");
        }
        try {
            $result = $this->busObj->confirmPayment($order_id, $mobile);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        if ($result) {
            return Show::json_show(Status::mini_normal, "ok", $result);
        } else {
            return Show::json_show(Status::mini_error, "业务失败");
        }
    }

    /**
     * @return \think\response\Json
     */
    public function getOrderListInfo()
    {
        $openid = $this->request->param('openid', '', 'trim');
        $type = $this->request->param('type', 0, "intval");
        if ($type == 0) {
            return Show::json_show(Status::mini_error, "类型错误");
        }
        try {
            $order = $this->busObj->getOrderListInfo($openid, $type);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $order);
    }

    /**
     * @return \think\response\Json
     */
    public function getOrderDetailByOrderId()
    {
        $order_id = $this->request->param('order_id', '', 'trim');
        if (empty($order_id)) {
            return Show::json_show(Status::mini_error, "订单号错误");
        }
        try {
            $order = $this->busObj->getOrderDetailByOrderId($order_id);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $order);
    }

    /**
     * @return \think\response\Json
     */
    public function delOrder()
    {
        $order_id = $this->request->param('order_id', '', 'trim');
        if (empty($order_id)) {
            return Show::json_show(Status::mini_error, "订单错误");
        }
        try {
            $order = $this->busObj->delOrder($order_id);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        if ($order) {
            return Show::json_show(Status::mini_normal, "删除成功");
        } else {
            return Show::json_show(Status::mini_error, "删除失败");
        }
    }

    public function getOrderByListInfo()
    {
        $openid = $this->request->param('openid', '', 'trim');
        try {
            $lists = $this->busObj->getOrderListByInfo($openid);
        } catch (\Exception $e) {
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        return Show::json_show(Status::mini_normal, "ok", $lists);
    }
}